home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / DISKVID.C < prev    next >
C/C++ Source or Header  |  1993-07-08  |  24KB  |  768 lines

  1. /*
  2.    "Disk-Video" (and RAM-Video and Expanded-Memory Video) routines
  3.  
  4.    Reworked with fast caching July '90 by Pieter Branderhorst.
  5.    (I'm proud of this cache handler, had to get my name on it!)
  6.    Caution when modifying any code in here:  bugs are possible which
  7.    slow the cache substantially but don't cause incorrect results.
  8.    Do timing tests for a variety of situations after any change.
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #ifndef XFRACT
  14. #include <dos.h>
  15. #endif
  16. #include <string.h>
  17. #include "fractint.h"
  18. #include "prototyp.h"
  19.  
  20. extern int sxdots, sydots, colors;
  21. extern int dotmode;      /* video access method, 11 if really disk video */
  22. extern int diskisactive;  /* set by fractint to disable re-init */
  23. extern int debugflag;
  24. extern int diskflag;
  25.  
  26. #define BOXROW     6
  27. #define BOXCOL     11
  28. #define BOXWIDTH 57
  29. #define BOXDEPTH 12
  30.  
  31. int disk16bit=0;       /* storing 16 bit values for continuous potential */
  32.  
  33. extern int Shadowing, AntiAliasing;
  34.  
  35. static int timetodisplay;
  36. static FILE *fp = NULL;
  37. static int disktarga;
  38.  
  39. #define BLOCKLEN 64    /* must be a power of 2, must match next */
  40. #define BLOCKSHIFT 6    /* must match above */
  41. #define CACHEMIN 4    /* minimum cache size in Kbytes */
  42. #define CACHEMAX 64    /* maximum cache size in Kbytes */
  43. #define FREEMEM  20    /* try to leave this much far memory unallocated */
  44. #define HASHSIZE 1024    /* power of 2, near CACHEMAX/(BLOCKLEN+8) */
  45.  
  46. static struct cache {    /* structure of each cache entry */
  47.    long offset;            /* pixel offset in image */
  48.    BYTE pixel[BLOCKLEN];  /* one pixel per byte (this *is* faster) */
  49.    unsigned int hashlink;       /* ptr to next cache entry with same hash */
  50.    unsigned int dirty : 1;       /* changed since read? */
  51.    unsigned int lru : 1;       /* recently used? */
  52.    } far *cache_end, far *cache_lru, far *cur_cache;
  53.  
  54. static struct cache far *cache_start = NULL;
  55. static long high_offset;       /* highwater mark of writes */
  56. static long seek_offset;       /* what we'll get next if we don't seek */
  57. static long cur_offset;        /* offset of last block referenced */
  58. static int cur_row;
  59. static long cur_row_base;
  60. static unsigned int far *hash_ptr = NULL;
  61. static int pixelshift;
  62. static int headerlength;
  63. static unsigned int rowsize = 0;   /* doubles as a disk video not ok flag */
  64. static unsigned int colsize;       /* sydots, *2 when pot16bit */
  65.  
  66. static BYTE far *charbuf = NULL;   /* currently used only with XMM */
  67.  
  68. /* For expanded memory: */
  69. static BYTE far *expmemoryvideo;
  70. static int oldexppage,expoffset;
  71. static unsigned int emmhandle = 0;
  72.  
  73. /* For extended memory: */
  74. static unsigned int xmmhandle = 0;
  75. static long extoffset;
  76. static BYTE far *extbufptr, far *endreadbuf, far *endwritebuf;
  77. #define XMMREADLEN 64    /* amount transferred from extended mem at once */
  78. #define XMMWRITELEN 256 /* max amount transferred to extended mem at once,
  79.                must be a factor of 1024 and >= XMMREADLEN */
  80.  
  81. int  _fastcall common_startdisk(long newrowsize, long newcolsize, int colors);
  82. static void _fastcall near findload_cache(long);
  83. static struct cache far * _fastcall near find_cache(long);
  84. static void near write_cache_lru(void);
  85. static void (_fastcall near *put_char)(BYTE);
  86. static void _fastcall near disk_putc(BYTE);
  87. static void _fastcall near exp_putc(BYTE);
  88. static void _fastcall near ext_putc(BYTE);
  89. static BYTE (near *get_char)();
  90. static BYTE near disk_getc();
  91. static BYTE near exp_getc();
  92. static BYTE near ext_getc();
  93. static void (_fastcall near *doseek)(long);
  94. static void _fastcall near disk_seek(long);
  95. static void _fastcall near exp_seek(long);
  96. static void _fastcall near ext_seek(long);
  97.  
  98. int made_dsktemp = 0;
  99. char diskfilename[] = {"FRACTINT.$$$"};
  100. int startdisk()
  101. {
  102.    if (!diskisactive)
  103.       return(0);
  104.    headerlength = disktarga = 0;
  105.    return (common_startdisk(sxdots,sydots,colors));
  106.    }
  107.  
  108. int pot_startdisk()
  109. {
  110.    int i;
  111.    if (dotmode == 11) /* ditch the original disk file */
  112.       enddisk();
  113.    else
  114.       showtempmsg("clearing 16bit pot work area");
  115.    headerlength = disktarga = 0;
  116.    i = common_startdisk(sxdots,sydots<<1,colors);
  117.    cleartempmsg();
  118.    if (i == 0)
  119.       disk16bit = 1;
  120.    return (i);
  121.    }
  122.  
  123. int targa_startdisk(FILE *targafp,int overhead)
  124. {
  125.    int i;
  126.    if (dotmode == 11) { /* ditch the original disk file, make just the targa */
  127.       enddisk();      /* close the 'screen' */
  128.       setnullvideo(); /* set readdot and writedot routines to do nothing */
  129.       }
  130.    headerlength = overhead;
  131.    fp = targafp;
  132.    disktarga = 1;
  133.    i = common_startdisk(sxdots*3,sydots,colors);
  134.    high_offset = 100000000L; /* targa not necessarily init'd to zeros */
  135.    return (i);
  136. }
  137.  
  138. extern char boxy[];
  139.  
  140. int _fastcall common_startdisk(long newrowsize, long newcolsize, int colors)
  141. {
  142.    int i,success;
  143.    long memorysize;
  144.    unsigned int far *fwd_link;
  145.    struct cache far *ptr1;
  146.    long longtmp;
  147.    unsigned int cache_size;
  148.    int exppages;
  149.    struct XMM_Move MoveStruct;
  150.    BYTE far *tempfar;
  151.  
  152.    if (diskflag)
  153.       enddisk();
  154.    if (dotmode == 11) { /* otherwise, real screen also in use, don't hit it */
  155.       char buf[20];
  156.       helptitle();
  157.       setattr(1,0,C_DVID_BKGRD,24*80);    /* init rest to background */
  158.       for (i = 0; i < BOXDEPTH; ++i)
  159.      setattr(BOXROW+i,BOXCOL,C_DVID_LO,BOXWIDTH);  /* init box */
  160.       putstring(BOXROW+2,BOXCOL+4,C_DVID_HI,"'Disk-Video' mode");
  161.       putstring(BOXROW+4,BOXCOL+4,C_DVID_LO,"Screen resolution: ");
  162.       sprintf(buf,"%d x %d",sxdots,sydots);
  163.       putstring(-1,-1,C_DVID_LO,buf);
  164.       if (disktarga)
  165.      putstring(-1,-1,C_DVID_LO,"  24 bit Targa");
  166.       else {
  167.      putstring(-1,-1,C_DVID_LO,"  Colors: ");
  168.      sprintf(buf,"%d",colors);
  169.      putstring(-1,-1,C_DVID_LO,buf);
  170.      }
  171.       putstring(BOXROW+8,BOXCOL+4,C_DVID_LO,"Status:");
  172.       dvid_status(0,"clearing the 'screen'");
  173.       }
  174.    cur_offset = seek_offset = high_offset = -1;
  175.    cur_row    = -1;
  176.    if (disktarga)
  177.       pixelshift = 0;
  178.    else {
  179.       pixelshift = 3;
  180.       i = 2;
  181.       while (i < colors) {
  182.      i *= i;
  183.      --pixelshift;
  184.      }
  185.       }
  186.    timetodisplay = 1000;  /* time-to-display-status counter */
  187.  
  188.    /* allocate cache: try for the max; leave FREEMEMk free if we can get
  189.       that much or more; if we can't get that much leave 1/2 of whatever
  190.       there is free; demand a certain minimum or nogo at all */
  191.    for (cache_size = CACHEMAX; cache_size >= CACHEMIN; --cache_size) {
  192.       longtmp = (cache_size < FREEMEM) ? (long)cache_size << 11
  193.                        : (long)(cache_size+FREEMEM) << 10;
  194.       if ((tempfar = farmemalloc(longtmp))) {
  195.      farmemfree(tempfar);
  196.      break;
  197.      }
  198.       }
  199.    longtmp = (long)cache_size << 10;
  200.    cache_start = (struct cache far *)farmemalloc(longtmp);
  201.    if (cache_size == 64)
  202.       --longtmp; /* safety for next line */
  203.    cache_end = (cache_lru = cache_start) + longtmp / sizeof(*cache_start);
  204.    hash_ptr  = (unsigned int far *)farmemalloc((long)(HASHSIZE<<1));
  205.    if (cache_start == NULL || hash_ptr == NULL) {
  206.       static char far msg[]={"*** insufficient free memory for cache buffers ***"};
  207.       stopmsg(0,msg);
  208.       return(-1);
  209.       }
  210.    if (dotmode == 11) {
  211.       char buf[50];
  212.       sprintf(buf,"Cache size: %dK\n\n",cache_size);
  213.       putstring(BOXROW+6,BOXCOL+4,C_DVID_LO,buf);
  214.       }
  215.  
  216.    /* preset cache to all invalid entries so we don't need free list logic */
  217.    for (i = 0; i < HASHSIZE; ++i)
  218.       hash_ptr[i] = 0xffff; /* 0xffff marks the end of a hash chain */
  219.    longtmp = 100000000L;
  220.    for (ptr1 = cache_start; ptr1 < cache_end; ++ptr1) {
  221.       ptr1->dirty = ptr1->lru = 0;
  222.       fwd_link = hash_ptr
  223.      + (((unsigned short)(longtmp+=BLOCKLEN) >> BLOCKSHIFT) & (HASHSIZE-1));
  224.       ptr1->offset = longtmp;
  225.       ptr1->hashlink = *fwd_link;
  226.       *fwd_link = (char far *)ptr1 - (char far *)cache_start;
  227.       }
  228.  
  229.    memorysize = (long)(newcolsize) * newrowsize;
  230.    if ((i = (short)memorysize & (BLOCKLEN-1)) != 0)
  231.       memorysize += BLOCKLEN - i;
  232.    memorysize >>= pixelshift;
  233.    diskflag = 1;
  234.    rowsize = (unsigned int) newrowsize;
  235.    colsize = (unsigned int) newcolsize;
  236.  
  237.    if (debugflag != 420 && debugflag != 422 /* 422=